package com.mysmartphone.views;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.SubMenu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.Toast;
import com.mysmartphone.adapter.MenuListAdapter;
import com.mysmartphone.bd.MySQLiteHelper;
import com.mysmartphone.row.MenuListaItem;
import com.mysmartphone.tools.Tools;
import com.mysmartphone.vistas.R;
import com.mysmartphone.vo.Phone;

public class MainActivity extends Activity implements OnQueryTextListener {
	private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;
	private ActionBarDrawerToggle mDrawerToggle;
	private SearchView searchView;
	private CharSequence mDrawerTitle;
	private CharSequence mTitle;
	private String[] titleMenu;
	private TypedArray menuIcons;
	private ArrayList<MenuListaItem> listaMenuItem;
	private MenuListAdapter adapter;
	private ActionBar actionBar;
	private ArrayList<Phone>listPhone = new ArrayList<Phone>();
	private boolean backResumen = false;
	private int iconActionBarClose;
	private Context context;
	MySQLiteHelper db;
	private ProgressDialog pDialog;
	JSONArray jsonListPhone = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		context = getApplicationContext();
		
		db = new MySQLiteHelper(context);
		
		//db.deleteDatabase();
		
		Long totalFavoritos = db.getTotalCategory(db.FAVORITE);
		
		
		mTitle = mDrawerTitle = getTitle();
		titleMenu = getResources().getStringArray(R.array.nav_drawer_items);

		menuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);

		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

		listaMenuItem = new ArrayList<MenuListaItem>();

		listaMenuItem.add(new MenuListaItem(titleMenu[0], menuIcons.getResourceId(0, -1)));
		listaMenuItem.add(new MenuListaItem(titleMenu[1], menuIcons.getResourceId(1, -1)));
		listaMenuItem.add(new MenuListaItem(titleMenu[2], menuIcons.getResourceId(2, -1),true,String.valueOf(totalFavoritos)));
		//listaMenuItem.add(new MenuListaItem(titleMenu[3], menuIcons.getResourceId(3, -1), true, "22"));
		//listaMenuItem.add(new MenuListaItem(titleMenu[4], menuIcons.getResourceId(4, -1)));
		listaMenuItem.add(new MenuListaItem(titleMenu[5], menuIcons.getResourceId(5, -1)));

		menuIcons.recycle();

		mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

		adapter = new MenuListAdapter(context, listaMenuItem);
		mDrawerList.setAdapter(adapter);

		actionBar = getActionBar();

		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setHomeButtonEnabled(true);
		
		
		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
				R.drawable.ic_drawer, R.string.app_name, R.string.app_name) {
			public void onDrawerClosed(View view) {
				actionBar.setTitle(mTitle);
				actionBar.setIcon(iconActionBarClose);
				invalidateOptionsMenu();
			}

			public void onDrawerOpened(View drawerView) {
				actionBar.setTitle(mDrawerTitle);
				actionBar.setIcon(R.drawable.home_img);
				invalidateOptionsMenu();
			}
		};

		mDrawerLayout.setDrawerListener(mDrawerToggle);
		if (savedInstanceState == null) {
			String param[] = {"http://smdomain.esy.es/index.php","0"};
			new LoadPhone().execute(param);
			//listPhone = getListPhone();
		}

	}

	public class SlideMenuClickListener implements ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			showView(position,listPhone);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		MenuItem menuItem = menu.findItem(R.id.action_search);

		searchView = (SearchView) menuItem.getActionView();
		searchView.setOnQueryTextListener(this);
		searchView.setQueryHint(getString(R.string.search));
		
		createMenuFilter(menu);
		
		return true;
	}

	private void createMenuFilter(Menu menu) {
		//SubMenu sub = new
		HashMap<Long, String> bands = new HashMap<Long, String>();
		
		//Cursor
		ArrayList<String> brands= new ArrayList<String>();
		brands.add("Nokia");
		brands.add("HTC");
		brands.add("Lenovo");
		brands.add("Zopo");
		brands.add("Samsung");
		brands.add("Cubot");

		SubMenu subMenuBrand = menu.getItem(1).getSubMenu().getItem(0).getSubMenu();
		for(int i=0;i <brands.size();i++){
			subMenuBrand.add(Menu.NONE, 1, Menu.NONE, brands.get(i)).setOnMenuItemClickListener(new OnMenuItemClickListener() {
				
				@Override
				public boolean onMenuItemClick(MenuItem item) {
					onQueryTextSubmit(item.getTitle().toString()+"=0");
					return true;
				}
			});
		}
		
		ArrayList<String> cpu= new ArrayList<String>();
		cpu.add("MTK657");
		cpu.add("MTK6577");
		cpu.add("MTK6575");
		cpu.add("MTK6572");
		cpu.add("MTK6582");
		cpu.add("SnapDragon");
		SubMenu subMenuCpu = menu.getItem(1).getSubMenu().getItem(1).getSubMenu();
		for(int i=0;i <cpu.size();i++){
			subMenuCpu.add(Menu.NONE, 1, Menu.NONE, cpu.get(i)).setOnMenuItemClickListener(new OnMenuItemClickListener() {
				
				@Override
				public boolean onMenuItemClick(MenuItem item) {
					onQueryTextSubmit(item.getTitle().toString()+"=1");
					return true;
				}
			});
		}
		
		ArrayList<String> ram= new ArrayList<String>();
		ram.add("256MB");
		ram.add("512MB");
		ram.add("1GB");
		ram.add("2GB");
		ram.add("3GB");
		ram.add("4GB");
		SubMenu subMenuRAM = menu.getItem(1).getSubMenu().getItem(2).getSubMenu();
		for(int i=0;i <ram.size();i++){
			subMenuRAM.add(Menu.NONE, 1, Menu.NONE, ram.get(i)).setOnMenuItemClickListener(new OnMenuItemClickListener() {
				
				@Override
				public boolean onMenuItemClick(MenuItem item) {
					onQueryTextSubmit(item.getTitle().toString()+"=2");
					return true;
				}
			});
		}
		
		ArrayList<String> rom= new ArrayList<String>();
		rom.add("256MB");
		rom.add("512MB");
		rom.add("1GB");
		rom.add("2GB");
		rom.add("3GB");
		rom.add("4GB");
		SubMenu subMenuROM= menu.getItem(1).getSubMenu().getItem(3).getSubMenu();
		for(int i=0;i <rom.size();i++){
			subMenuROM.add(Menu.NONE, 1, Menu.NONE, rom.get(i)).setOnMenuItemClickListener(new OnMenuItemClickListener() {
				
				@Override
				public boolean onMenuItemClick(MenuItem item) {
					onQueryTextSubmit(item.getTitle().toString()+"=3");
					return true;
				}
			});
		}
		
		ArrayList<String> backCamera= new ArrayList<String>();
		backCamera.add("1MP");
		backCamera.add("2MP");
		backCamera.add("3MP");
		backCamera.add("4MP");
		backCamera.add("5MP");
		backCamera.add("8MP");
		backCamera.add("14MP");
		backCamera.add("20MP");
		backCamera.add("40MP");
		SubMenu subMenuBackCamera= menu.getItem(1).getSubMenu().getItem(4).getSubMenu();
		for(int i=0;i <backCamera.size();i++){
			subMenuBackCamera.add(Menu.NONE, 1, Menu.NONE, backCamera.get(i)).setOnMenuItemClickListener(new OnMenuItemClickListener() {
				
				@Override
				public boolean onMenuItemClick(MenuItem item) {
					onQueryTextSubmit(item.getTitle().toString()+"=4");
					return true;
				}
			});
		}
		
		ArrayList<String> screen= new ArrayList<String>();
		screen.add("3.8"+" "+getString(R.string.inches));
		screen.add("4.0"+" "+getString(R.string.inches));
		screen.add("4.5"+" "+getString(R.string.inches));
		screen.add("5.0"+" "+getString(R.string.inches));
		screen.add("5.5"+" "+getString(R.string.inches));
		screen.add("6.0"+" "+getString(R.string.inches));
		
		SubMenu subMenuScreen= menu.getItem(1).getSubMenu().getItem(5).getSubMenu();
		for(int i=0;i <screen.size();i++){
			subMenuScreen.add(Menu.NONE, 1, Menu.NONE, screen.get(i)).setOnMenuItemClickListener(new OnMenuItemClickListener() {
				
				@Override
				public boolean onMenuItemClick(MenuItem item) {
					onQueryTextSubmit(item.getTitle().toString()+"=5");
					return true;
				}
			});
		}
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}
		switch (item.getItemId()) {
//		case R.id.brand:
//			
//			item.getMenuInfo();
//			item.getTitle();
//			
//			System.out.println("");
//			
//			return true;
//		case R.id.core:
//			return true;
//		case R.id.ram:
//			return true;
//		case R.id.rom:
//			return true;
//		case R.id.backCamera:
//			return true;
//		case R.id.screen:
//			return true;
			
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	public boolean onQueryTextChange(String newText) {
		// if (TextUtils.isEmpty(newText)) {
		// mListView.clearTextFilter();
		// } else {
		// mListView.setFilterText(newText.toString());
		// }
		return true;
	}

	public boolean onQueryTextSubmit(String query) {
		ArrayList<Phone> auxList = new ArrayList<Phone>();
		String data[] = query.split("=");
		
		String text = data[0];
		int posicion = Integer.parseInt(data[1]);
		
		for (Phone auxPhone:listPhone) {			
			switch (posicion) {
			case 0:
				if(auxPhone.getBrand().equals(text)){
					auxList.add(auxPhone);
				}
				break;
			case 1:
				if(auxPhone.getNameCore().equals(text)){
					auxList.add(auxPhone);
				}
				break;
			case 2:
				text = text.replace(getString(R.string.mb),"");
				text = text.replace(getString(R.string.gb),"");
				int ram =Integer.parseInt(text);
				if(auxPhone.getMemoryRAM()==ram){
					auxList.add(auxPhone);
				}
				break;
			case 3:
				text = text.replace(getString(R.string.mb),"");
				text = text.replace(getString(R.string.gb),"");
				int rom =Integer.parseInt(text);
				if(auxPhone.getMemoryROM()==rom){
					auxList.add(auxPhone);
				}
				break;
			case 4:
				text = text.replace(getString(R.string.mp),"");
				double backCamera = Double.parseDouble(text);
				if(auxPhone.getBackCamera()==backCamera){
					auxList.add(auxPhone);
				}
				break;
			case 5:
				text = text.replace(getString(R.string.inches),"");
				double screen = Double.parseDouble(text);
				if(auxPhone.getScreenSize()==screen){
					auxList.add(auxPhone);
				}
				break;

			default:
				
				break;
			}

		}
		listPhone = auxList;
		showView(0, listPhone);

		return false;
	}


	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		//boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
		// menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
		return super.onPrepareOptionsMenu(menu);
	}

	public void showView(int position,ArrayList<Phone>listPhone) {
		Fragment fragment = null;
		Bundle bundle = new Bundle();
		backResumen = false;
		switch (position) {
		case 0:
			//Busquedas
			fragment = new SearchFragment();
			bundle.putSerializable("listPhone", (Serializable) listPhone);
			fragment.setArguments(bundle);
            actionBar.setIcon(R.drawable.search_img);
            iconActionBarClose = R.drawable.search_img;
			break;
		case 1:
			//comparaciones
			fragment = new PhoneCompareFragment();
			actionBar.setIcon(R.drawable.compare_img);
			iconActionBarClose = R.drawable.compare_img;
			break;

		case 2:
			//favoritos
			fragment = new FavoriteFragment();
			bundle.putSerializable("listPhone", (Serializable) listPhone);
			fragment.setArguments(bundle);
			actionBar.setIcon(R.drawable.favoritos_img);
			iconActionBarClose = R.drawable.favoritos_img;
			break;
		case 3:
			fragment = new SettingsFragment();
			actionBar.setIcon(R.drawable.settings_img);
			iconActionBarClose = R.drawable.settings_img;
			break;
		case 4:
			//mis busquedas
			//fragment = new FindPeopleFragment();
			break;
		case 5:
			//mis comparaciones
			//fragment = new SettingsFragment();
			break;
		case 6 :
			fragment = new PhoneResumenFragment();
			bundle.putSerializable("phone",(Serializable) listPhone.get(0));
			fragment.setArguments(bundle);
			setTitle(titleMenu[position]);
			actionBar.setIcon(R.drawable.features_img);
			iconActionBarClose = R.drawable.features_img;
			backResumen = true;
			break;
		default:
			break;
		}

		if (fragment != null) {
			FragmentManager fragmentManager = getFragmentManager();
			fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
			
			if(position !=6){
				mDrawerList.setItemChecked(position, true);
				mDrawerList.setSelection(position);
				setTitle(titleMenu[position]);
				mDrawerLayout.closeDrawer(mDrawerList);
			}

		} else {
			Log.e("MainActivity", "Error in creating fragment");
		}
	}

	@Override
	public void setTitle(CharSequence title) {
		mTitle = title;
		actionBar.setTitle(mTitle);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

	private ArrayList<Phone> getListPhone() {

		ArrayList<Phone> listPhone = new ArrayList<Phone>();

		Phone phone = new Phone();
		phone.setLogo(R.drawable.cubot);
		phone.setIdPhone(3);
		phone.setBrand("Cubot");
		phone.setGama("Galaxy");
		phone.setModel("C11");
		phone.setNameCore("MTK657");
		phone.setSpeedCore(1.5);
		phone.setSpeedDesCore("Hz");
		phone.setScreenSize(5);
		phone.setMemoryROM(16.0);
		phone.setMemoryDesRom("MB");
		phone.setMemoryRAM(1.0);
		phone.setMemoryDesRam("GB");
		phone.setOperatingSystem("Android 4.2");
		//phone.setImage(Tools.getDownloadImage(context,"http://www.comprarmovilya.com/wp-content/uploads/2013/04/iphone5.jpg"));
		phone.setBackCamera(14.4);
		phone.setFrontCamera(5);
		phone.setCameraDes("MP");
		phone.setBattery(3600.0);
		phone.setBatteryDes("mAh");
		phone.setDimensions("25x45x71");
		phone.setBluetooth(false);
		phone.setWifi(true);
		phone.setConectivity("WCDMA");
		phone.setFrecuency("3G: 9000 2G 4000");
		phone.setGps(true);
		phone.setBechmark(150000D);
		phone.setBluetooth(false);
		phone.setNumberCore(8);
		phone.setNumberDesCore("Octacore");
		phone.setScreenTecnology("OGS");
		phone.setYear(2014);
		phone.setPrice(455D);
		phone.setNumberSim(2);
		phone.setResolution("540x960");
		phone.setBechmark(10000D);
		listPhone.add(phone);
		
		phone = new Phone();
		phone.setLogo(R.drawable.cubot);
		phone.setIdPhone(4);
		phone.setGama("Galaxy");
		phone.setBrand("Cubot");
		phone.setModel("C11");
		phone.setNameCore("MTK657");
		phone.setSpeedCore(1.5);
		phone.setSpeedDesCore("Hz");
		phone.setScreenSize(5);
		phone.setMemoryROM(16.0);
		phone.setMemoryDesRom("MB");
		phone.setMemoryRAM(3.0);
		phone.setMemoryDesRam("GB");
		phone.setOperatingSystem("Android 4.2");
		//phone.setImage(R.drawable.img);
		phone.setBackCamera(14.4);
		phone.setFrontCamera(5);
		phone.setCameraDes("MP");
		phone.setBattery(3600.0);
		phone.setDimensions("25x45x71");
		phone.setBluetooth(false);
		phone.setWifi(true);
		phone.setConectivity("WCDMA");
		phone.setFrecuency("3G: 9000 2G 4000");
		phone.setGps(true);
		phone.setBechmark(120000D);
		listPhone.add(phone);

		phone = new Phone();
		phone.setLogo(R.drawable.cubot);
		phone.setIdPhone(5);
		phone.setBrand("Cubot");
		phone.setModel("C11");
		phone.setGama("Galaxy");
		phone.setNameCore("MTK657");
		phone.setSpeedCore(1.5);
		phone.setSpeedDesCore("Hz");
		phone.setScreenSize(5);
		phone.setMemoryROM(16.0);
		phone.setMemoryDesRom("MB");
		phone.setMemoryRAM(3.0);
		phone.setMemoryDesRam("GB");
		phone.setOperatingSystem("Android 4.2");
		//phone.setImage(R.drawable.img);
		phone.setBackCamera(14.4);
		phone.setFrontCamera(5);
		phone.setCameraDes("MP");
		phone.setBattery(3600.0);
		phone.setDimensions("25x45x71");
		phone.setBluetooth(false);
		phone.setWifi(true);
		phone.setConectivity("WCDMA");
		phone.setFrecuency("3G: 9000 2G 4000");
		phone.setGps(true);
		phone.setBechmark(130000D);
		listPhone.add(phone);

		phone = new Phone();
		phone.setLogo(R.drawable.cubot);
		phone.setIdPhone(6);
		phone.setBrand("HTC");
		phone.setModel("C11");
		phone.setGama("Galaxy");
		phone.setNameCore("MTK657");
		phone.setSpeedCore(1.5);
		phone.setSpeedDesCore("Hz");
		phone.setScreenSize(5);
		phone.setMemoryROM(16.0);
		phone.setMemoryDesRom("MB");
		phone.setMemoryRAM(4.0);
		phone.setMemoryDesRam("GB");
		phone.setOperatingSystem("Android 4.2");
		//phone.setImage(R.drawable.img);
		phone.setBackCamera(14.4);
		phone.setFrontCamera(5.0);
		phone.setCameraDes("MP");
		phone.setBattery(3600.0);
		phone.setDimensions("25x45x71");
		phone.setBluetooth(false);
		phone.setWifi(true);
		phone.setConectivity("WCDMA");
		phone.setFrecuency("3G: 9000 2G 4000");
		phone.setGps(true);
		phone.setBechmark(140000D);
		listPhone.add(phone);

		phone = new Phone();
		phone.setLogo(R.drawable.cubot);
		phone.setIdPhone(7);
		phone.setBrand("HTC");
		phone.setModel("C11");
		phone.setGama("Galaxy");
		phone.setNameCore("MTK657");
		phone.setSpeedCore(1.5);
		phone.setSpeedDesCore("Hz");
		phone.setScreenSize(5);
		phone.setMemoryROM(16.0);
		phone.setMemoryDesRom("MB");
		phone.setMemoryRAM(2.0);
		phone.setMemoryDesRam("GB");
		phone.setOperatingSystem("Android 4.2");
		//phone.setImage(R.drawable.img);
		phone.setBackCamera(14.4);
		phone.setFrontCamera(5);
		phone.setCameraDes("MP");
		phone.setBattery(3600.0);
		phone.setDimensions("25x45x71");
		phone.setBluetooth(false);
		phone.setWifi(true);
		phone.setConectivity("WCDMA");
		phone.setFrecuency("3G: 9000 2G 4000");
		phone.setGps(true);
		phone.setBechmark(150000D);
		listPhone.add(phone);

		phone = new Phone();
		phone.setLogo(R.drawable.cubot);
		phone.setIdPhone(8);
		phone.setBrand("HTC");
		phone.setModel("C11");
		phone.setNameCore("MTK657");
		phone.setGama("Galaxy");
		phone.setSpeedCore(1.5);
		phone.setSpeedDesCore("Hz");
		phone.setScreenSize(5);
		phone.setMemoryROM(16.0);
		phone.setMemoryDesRom("MB");
		phone.setMemoryRAM(4.0);
		phone.setMemoryDesRam("GB");
		phone.setOperatingSystem("Android 4.2");
		//phone.setImage(R.drawable.img);
		phone.setBackCamera(14.4);
		phone.setFrontCamera(5.0);
		phone.setCameraDes("MP");
		phone.setBattery(3600.0);
		phone.setDimensions("25x45x71");
		phone.setBluetooth(false);
		phone.setWifi(true);
		phone.setConectivity("WCDMA");
		phone.setFrecuency("3G: 9000 2G 4000");
		phone.setGps(true);
		phone.setBechmark(160000D);
		listPhone.add(phone);

		phone = new Phone();
		phone.setLogo(R.drawable.cubot);
		phone.setIdPhone(9);
		phone.setBrand("HTC");
		phone.setModel("C11");
		phone.setNameCore("MTK657");
		phone.setSpeedCore(1.5);
		phone.setSpeedDesCore("Hz");
		phone.setScreenSize(5);
		phone.setMemoryROM(16.0);
		phone.setGama("Galaxy");
		phone.setMemoryDesRom("MB");
		phone.setMemoryRAM(2.0);
		phone.setMemoryDesRam("GB");
		phone.setOperatingSystem("Android 4.2");
		//phone.setImage(R.drawable.img);
		phone.setBackCamera(14.4);
		phone.setFrontCamera(5);
		phone.setCameraDes("MP");
		phone.setBattery(3600.0);
		phone.setDimensions("25x45x71");
		phone.setBluetooth(false);
		phone.setWifi(true);
		phone.setConectivity("WCDMA");
		phone.setFrecuency("3G: 9000 2G 4000");
		phone.setGps(true);
		phone.setBechmark(170000D);
		listPhone.add(phone);

		phone = new Phone();
		phone.setLogo(R.drawable.cubot);
		phone.setIdPhone(10);
		phone.setBrand("HTC");
		phone.setGama("Galaxy");
		phone.setModel("C11");
		phone.setNameCore("MTK657");
		phone.setSpeedCore(1.5);
		phone.setSpeedDesCore("Hz");
		phone.setScreenSize(5);
		phone.setMemoryROM(16.0);
		phone.setMemoryDesRom("MB");
		phone.setMemoryRAM(1.0);
		phone.setMemoryDesRam("GB");
		phone.setOperatingSystem("Android 4.2");
		//phone.setImage(R.drawable.img);
		phone.setBackCamera(14.4);
		phone.setFrontCamera(5);
		phone.setCameraDes("MP");
		phone.setBattery(3600.0);
		phone.setDimensions("25x45x71");
		phone.setBluetooth(false);
		phone.setWifi(true);
		phone.setConectivity("WCDMA");
		phone.setFrecuency("3G: 9000 2G 4000");
		phone.setGps(true);
		phone.setBechmark(180000D);
		listPhone.add(phone);

		phone = new Phone();
		phone.setLogo(R.drawable.cubot);
		phone.setIdPhone(11);
		phone.setBrand("Samsung");
		phone.setGama("Galaxy");
		phone.setModel("C11");
		phone.setNameCore("MTK657");
		phone.setSpeedCore(1.5);
		phone.setSpeedDesCore("Hz");
		phone.setScreenSize(5);
		phone.setMemoryROM(16.0);
		phone.setMemoryDesRom("MB");
		phone.setMemoryRAM(2.0);
		phone.setMemoryDesRam("GB");
		phone.setOperatingSystem("Android 4.2");
		//phone.setImage(R.drawable.img);
		phone.setBackCamera(14.4);
		phone.setFrontCamera(5);
		phone.setCameraDes("MP");
		phone.setBattery(3600.0);
		phone.setDimensions("25x45x71");
		phone.setBluetooth(false);
		phone.setWifi(true);
		phone.setConectivity("WCDMA");
		phone.setFrecuency("3G: 9000 2G 4000");
		phone.setGps(true);
		phone.setBechmark(190000D);
		listPhone.add(phone);

		phone = new Phone();
		phone.setLogo(R.drawable.cubot);
		phone.setIdPhone(12);
		phone.setBrand("Samsung");
		phone.setModel("C11");
		phone.setNameCore("MTK657");
		phone.setSpeedCore(1.5);
		phone.setGama("Galaxy");
		phone.setSpeedDesCore("Hz");
		phone.setScreenSize(5.5);
		phone.setMemoryROM(16.0);
		phone.setMemoryDesRom("MB");
		phone.setMemoryRAM(1.0);
		phone.setMemoryDesRam("GB");
		phone.setOperatingSystem("Android 4.2");
		//phone.setImage(R.drawable.img);
		phone.setBackCamera(14.4);
		phone.setFrontCamera(5);
		phone.setCameraDes("MP");
		phone.setBattery(3600.0);
		phone.setDimensions("25x45x71");
		phone.setBluetooth(false);
		phone.setWifi(true);
		phone.setConectivity("WCDMA");
		phone.setFrecuency("3G: 9000 2G 4000");
		phone.setGps(true);
		phone.setBechmark(20000D);
		listPhone.add(phone);

		phone = new Phone();
		phone.setLogo(R.drawable.cubot);
		phone.setIdPhone(13);
		phone.setBrand("Nokia");
		phone.setGama("Galaxy");
		phone.setModel("C11");
		phone.setNameCore("MTK657");
		phone.setSpeedCore(1.5);
		phone.setSpeedDesCore("Hz");
		phone.setScreenSize(5);
		phone.setMemoryROM(16.0);
		phone.setMemoryDesRom("MB");
		phone.setMemoryRAM(256.0);
		phone.setMemoryDesRam("GB");
		phone.setOperatingSystem("Android 4.2");
		//phone.setImage(R.drawable.img);
		phone.setBackCamera(14.4);
		phone.setFrontCamera(5);
		phone.setCameraDes("MP");
		phone.setBattery(3600.0);
		phone.setDimensions("25x45x71");
		phone.setBluetooth(false);
		phone.setWifi(true);
		phone.setConectivity("WCDMA");
		phone.setFrecuency("3G: 9000 2G 4000");
		phone.setGps(true);
		phone.setBechmark(210000D);
		listPhone.add(phone);

		phone = new Phone();
		phone.setLogo(R.drawable.cubot);
		phone.setIdPhone(14);
		phone.setBrand("Cubot");
		phone.setGama("Galaxy");
		phone.setModel("C11");
		phone.setNameCore("MTK657");
		phone.setSpeedCore(1.5);
		phone.setSpeedDesCore("Hz");
		phone.setScreenSize(5);
		phone.setMemoryROM(8.0);
		phone.setMemoryDesRom("MB");
		phone.setMemoryRAM(2.0);
		phone.setMemoryDesRam("GB");
		phone.setOperatingSystem("Android 4.2");
		//phone.setImage(R.drawable.img);
		phone.setBackCamera(14.4);
		phone.setFrontCamera(5);
		phone.setCameraDes("MP");
		phone.setBattery(3600.0);
		phone.setDimensions("25x45x71");
		phone.setBluetooth(false);
		phone.setWifi(true);
		phone.setConectivity("WCDMA");
		phone.setFrecuency("3G: 9000 2G 4000");
		phone.setGps(true);
		phone.setBechmark(220000D);
		listPhone.add(phone);

		phone = new Phone();
		phone.setLogo(R.drawable.cubot);
		phone.setIdPhone(15);
		phone.setBrand("Nokia");
		phone.setModel("C11");
		phone.setNameCore("MTK657");
		phone.setSpeedCore(1.5);
		phone.setGama("Galaxy");
		phone.setSpeedDesCore("Hz");
		phone.setScreenSize(4.5);
		phone.setMemoryROM(16.0);
		phone.setMemoryDesRom("MB");
		phone.setMemoryRAM(2.0);
		phone.setMemoryDesRam("GB");
		phone.setOperatingSystem("Android 4.2");
		//phone.setImage(R.drawable.img);
		phone.setBackCamera(5);
		phone.setFrontCamera(5);
		phone.setCameraDes("MP");
		phone.setBattery(3600.0);
		phone.setDimensions("25x45x71");
		phone.setBluetooth(false);
		phone.setWifi(true);
		phone.setConectivity("WCDMA");
		phone.setFrecuency("3G: 9000 2G 4000");
		phone.setGps(true);
		phone.setBechmark(230000D);
		listPhone.add(phone);
		return listPhone;

	}

	@Override
	protected void onResume() {
		super.onResume();
		
	}
	@Override
	public void finish() {
		if(backResumen){
			showView(0, listPhone);
			backResumen= false;
		}
		else {			
			super.finish();
		}
	}
	
	/**
	 * Background Async Task to Load all product by making HTTP Request
	 * */
	class LoadPhone extends AsyncTask<String, String, String> {

		int view =0;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(MainActivity.this);
			pDialog.setMessage(getString(R.string.loading));
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show(); 
		}

		/**
		 * getting All products from url
		 * */
		protected String doInBackground(String... args) {
			
			
			String url = args[0];
			this.view = Integer.parseInt(args[1]);
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			// getting JSON string from URL
			JSONObject json = Tools.makeHttpRequest(url, "POST", params);
			
			try {
				if(json!=null){
					jsonListPhone = json.getJSONArray("listPhone");
					listPhone = Tools.jsonToListPhone(jsonListPhone);
				}
				else{
					Toast toast = Toast.makeText(context, getString(R.string.errorConnection),50000);
					toast.show();
				}

			} catch (JSONException e) {
				Toast toast = Toast.makeText(context, getString(R.string.errorConnection),50000);
				toast.show();
				e.printStackTrace();
			}
			
//			String downloadUrl = "http://www.9ori.com/store/media/images/8ab579a656.jpg";
			String downloadUrl = "http://smdomain.esy.es/prueba/img.jpg";
			try {
				Bitmap bm = downloadBitmap(downloadUrl);
				for (int i = 0; i < listPhone.size(); i++) {
					
					
					if(bm!=null){

						listPhone.get(i).setImage(bm);
					}
					else{
						bm = BitmapFactory.decodeResource(getResources(), R.drawable.phone2);
						listPhone.get(i).setImage(bm);
					}
					
				}
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}

		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		protected void onPostExecute(String file_url) {
			// dismiss the dialog after getting all products
			pDialog.dismiss();
			// updating UI from Background Thread
			runOnUiThread(new Runnable() {
				public void run() {

				}
			});
			showView(0, listPhone);

		}
		
		private Bitmap downloadBitmap(String url) throws IOException {

			final DefaultHttpClient client = new DefaultHttpClient();
			HttpEntity entity = null;
			final HttpGet getRequest = new HttpGet(url);
			InputStream inputStream = null;
			try {

				HttpResponse response = client.execute(getRequest);

				final int statusCode = response.getStatusLine().getStatusCode();

				if (statusCode != HttpStatus.SC_OK) {
					Log.w("ImageDownloader", "Error " + statusCode
							+ " while retrieving bitmap from " + url);
					return null;

				}
				entity = response.getEntity();

				if (entity != null) {

					inputStream = entity.getContent();

					// decoding stream data back into image Bitmap that android
					// understands
					final Bitmap bitmap = BitmapFactory
							.decodeStream(inputStream);

					return bitmap;

				}
			} catch (Exception e) {
				// You Could provide a more explicit error message for
				// IOException
				getRequest.abort();
				Log.e("ImageDownloader", "Something went wrong while"
						+ " retrieving bitmap from " + url + e.toString());
			}
			return null;
		}

	}
}
